package com.br4mmie.asciistory.shop;

import com.br4mmie.asciistory.*;
import com.br4mmie.asciistory.mission.Inventory;
import com.br4mmie.asciistory.mission.Item;
import com.br4mmie.asciistory.ui.*;

/**
 * ShopWindow visualises the shop
 * and handles it's events
 * @author Bram Buurlage
 * 		   Pim Korschner
 */
public class ShopWindow extends Window
{
	private Shop shop;
	private int selX;
	private int selY;
	
	/**
	 * constructor
	 * @param shop the Shop that lies under the ShopWindow
	 */
	public ShopWindow(Shop shop) 
	{
		super(4, 4, ASCIIStoryActivity.getScreenWidth()-8, ASCIIStoryActivity.getScreenHeight()-8);
		this.shop = shop;
	}
	/**
	 * draw an inventory to image
	 * @param inv the inventory to draw
	 * @param X the X location to draw to
	 * @param Y the Y lcoation to draw to
	 * @param w the width in slots
	 * @param h the height in slots
	 * @param selYRange the y selection value that represents the first slot (for we draw multiple inventories)
	 */
	private void drawInventory(Inventory inv, int X, int Y, int w, int h, int selYRange)
	{
		int x=w, y=h;		
		
		image.line('-', X, (y*2)+Y, X+w*5, (y*2)+Y);
		image.line('|', (x*5)+X, Y, (x*5)+X, Y+h*2);
		for (y=0; y<h; y++)
		{
			image.line('-', X, (y*2)+Y, X+w*5, (y*2)+Y);
			for (x=0; x<w; x++)
			{
				image.line('|', (x*5)+X, Y, (x*5)+X, Y+h*2);
				
				int i = (x + y*w);
				if (i < inv.size())
				{
					Item item = inv.get(i).item;
					item.getImage().draw(image, X+2 + x*5, Y+1 + y*2, 0, 0, 1, 1);
				
					String amountStr = ""+inv.get(i).amount;
					image.putChar(amountStr.charAt(0), X+3+x*5, Y+1+y*2);
				}
				if (x == selX && y == selY-selYRange)
				{
					image.putChar('(', X+(x*5)+1, Y+(y*2)+1);
					image.putChar(')', X+(x*5)+4, Y+(y*2)+1);
				}
				for (int plusY=0; plusY<h+1; plusY++)
				{
					image.putChar('+', (x*5)+X, Y+(plusY*2));
				}
			}
		}
		for (int plusY=0; plusY<h+1; plusY++)
		{
			image.putChar('+', (x*5)+X, Y+(plusY*2));
		}
	}
	/**
	 * draws info about an item
	 * @param item the item to draw info about
	 */
	private void drawItemInfo(Item item)
	{
		int y=5;
		image.drawText(30, y++, 22, "-----Item info-----");
		y+=image.drawText(30, y++, 19, item.getName())+1;
		image.drawText(30, y++, 22, "-------------------");
		y+=image.drawText(30, y++, 19, item.getDescription())+1;
		image.drawText(30, y++, 22, "-------------------");
		y+=image.drawText(30, y++, 19, "Price: "+item.getGoldValue())+1;
		image.drawText(30, y++, 22, "-------------------");
	}
	/**
	 * draw the InventoryWindow
	 */
	public void draw(CharImage target)
	{
		image.fill(' ');
		image.line('-', 1, 2, image.getWidth(), 2);
		image.drawText(1, 1, image.getWidth()-2, "Shop");
		
		drawInventory(shop.seller, 2, 4, 5, 4, 0);
		drawInventory(shop.buyer,  2, 15, 5, 2, 4);
		
		image.drawText(2, 14, 20, "Gold: "+shop.buyer.getGold());
		
		int selected = selX + (selY)*5;
		if (selected < shop.seller.size() && selected >= 0)
		{
			drawItemInfo(shop.seller.get(selected).item);
		}
		selected = selX + (selY-4)*5;
		if (selected < shop.buyer.size() && selected >= 0)
		{
			drawItemInfo(shop.buyer.get(selected).item);
		}
		
		super.draw(target);
	}
	/**
	 * handle events. (selecting/buying/selling)
	 */
	public void handleEvents()
	{
		if (Keybindings.RIGHT.pressed())
		{
			if (selX < 4) selX++;
		}
		if (Keybindings.LEFT.pressed())
		{
			if (selX > 0) selX--;
		}
		if (Keybindings.UP.pressed())
		{
			if (selY > 0) selY--;
		}
		if (Keybindings.DOWN.pressed())
		{
			if (selY < 5) selY++;
		}
		if (Keybindings.INTERACT.pressed())
		{
			int selected = selX + (selY)*5;
			if (selected < shop.seller.size() && selected >= 0)
			{
				shop.buy(shop.seller.get(selected).item, shop.buyer.getGoldInv());
			}
			selected = selX + (selY-4)*5;
			if (selected < shop.buyer.size() && selected >= 0)
			{
				shop.sell(shop.buyer.get(selected), shop.buyer.getGoldInv());
			}
		}
		
		super.handleEvents();
	}
}
